using LibreHardwareMonitor.Hardware;
using System.Diagnostics;

namespace DesktopToy.Test;

/// <summary>
/// 测试获取硬件温度
/// </summary>
public class TestDeviceTemperature
{
    /// <summary>
    /// 测试所有硬件温度
    /// </summary>
    [Test]
    public void TestAllDeviceInfo()
    {
        var computer = new Computer
        {
            IsCpuEnabled = true,
            IsGpuEnabled = true,
            IsMemoryEnabled = true,
            IsMotherboardEnabled = true,
            IsControllerEnabled = true,
            IsNetworkEnabled = true,
            IsStorageEnabled = true,
            IsBatteryEnabled = true
        };
        
        computer.Open();

        foreach (var hardware in computer.Hardware)
        {
            GetHardwareInfo(hardware);
        }
        
        computer.Close();
    }

    private void GetHardwareInfo(IHardware hardware)
    {
        try
        {
            hardware.Update(); // 更新硬件数据
 
            // 根据硬件类型执行不同的操作
            switch (hardware.HardwareType)
            {
                case HardwareType.Cpu:
                    Console.WriteLine("CPU Information:");
                    break;
 
                case HardwareType.GpuIntel:
                case HardwareType.GpuNvidia:
                case HardwareType.GpuAmd:
                    Console.WriteLine("GPU Information:");
                    break;
 
                case HardwareType.Memory:
                    Console.WriteLine("Memory Information:");
                    break;
 
                case HardwareType.Motherboard:
                    Console.WriteLine("Motherboard Information:");
                    break;
 
                case HardwareType.Storage:
                    Console.WriteLine("Storage Information:");
                    break;
 
                case HardwareType.Network:
                    Console.WriteLine("Network Information:");
                    break;
 
                case HardwareType.SuperIO:
                    Console.WriteLine("SuperIO Information:");
                    break;
 
                default:
                    Console.WriteLine("Unknown Hardware Type.");
                    break;
            }
 
            // 遍历所有传感器，获取并打印传感器名称和值
            foreach (var sensor in hardware.Sensors)
            {
                string name = sensor.Name;
                float? value = sensor.Value;
 
                if (value.HasValue)
                {
                    Console.WriteLine($"  {name}: {value.Value}");
                }
            }
 
            // 递归处理子硬件信息
            foreach (var subHardware in hardware.SubHardware)
            {
                GetHardwareInfo(subHardware);
            }
        }
        catch (Exception ex)
        {
            // 捕获并打印异常信息
            Console.WriteLine($"Error: {ex.Message}");
        }
    }



    /// <summary>
    /// 测试所有硬件温度
    /// </summary>
    [Test]
    public void TestCpuTemperature()
    {
        var computer = new Computer
        {
            IsCpuEnabled = true,
            IsGpuEnabled = true,
            IsMemoryEnabled = true,
            IsMotherboardEnabled = true,
            IsControllerEnabled = true,
            IsNetworkEnabled = true,
            IsStorageEnabled = true,
            IsBatteryEnabled = true
        };

        computer.Open();

        foreach (var hardware in computer.Hardware)
        {
            if(hardware.HardwareType == HardwareType.Cpu)
            {
                hardware.Update();
                PrintCpuTemperature(hardware);
                break;
            }
        }

        computer.Close();
    }

    public void PrintCpuTemperature(IHardware hardware)
    {
        Debug.Assert(hardware.HardwareType == HardwareType.Cpu);

        foreach (var sensor in hardware.Sensors)
        {
            if (sensor.SensorType != SensorType.Temperature) continue;
            if (!sensor.Value.HasValue) continue;
            Console.WriteLine($"{sensor.Name} Min: {sensor.Min}, Value: {sensor.Value}, Max: {sensor.Max}");
        }
    }
}